package com.zhsj.OkeHomeConsumption.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.OkeHomeConsumptionApi;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.empty.ShareMemberBean;
import com.zhsj.OkeHomeConsumption.other.URLEntity;
import com.zhsj.OkeHomeConsumption.ui.activity.ApiServer;
import com.zhsj.OkeHomeConsumption.ui.activity.LoginActivity;
import com.zhsj.OkeHomeConsumption.utils.SharedPreferanceUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShareMemberFragment extends Fragment implements View.OnClickListener {


    private RecyclerView recycleview;
    private ArrayList<ShareMemberBean.DataBeanX.DataBean> shareMemberBeans;
    private static final String TAG = "ShareMemberFragment";
    private  ShareMemberAdapter shareMemberAdapter;
    private FormalUserInfo userInfo = null;
    private SharedPreferanceUtils mSharePreferanceUtils = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_share_member, container, false);
        initView(inflate);
        initdata2();
        return inflate;
    }

    private void initView(View inflate) {
        mSharePreferanceUtils = new SharedPreferanceUtils(getActivity());
        userInfo =   Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        recycleview = (RecyclerView) inflate.findViewById(R.id.recycleview);
        shareMemberBeans=new ArrayList<>();
        shareMemberAdapter=new ShareMemberAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recycleview.setLayoutManager(linearLayoutManager);
        recycleview.setAdapter(shareMemberAdapter);
    }

    @Override
    public void onClick(View view) {

    }

    public  class  ShareMemberAdapter extends BaseQuickAdapter<ShareMemberBean.DataBeanX.DataBean, BaseViewHolder> {
        public ShareMemberAdapter() {
            super(R.layout.invatemember_item, shareMemberBeans);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, final ShareMemberBean.DataBeanX.DataBean item) {
            helper.setText(R.id.title,item.getTitle());
            Glide.with(getActivity()).load(item.getImagePath()).into((ImageView) helper.getView(R.id.image));
            helper.getView(R.id.item_linear).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mSharePreferanceUtils.getToken().equals("")) {
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                    }else{
                        Intent intent = new Intent(getActivity(), SharedWebviewActivity.class)
                                .putExtra("userInfo", userInfo)
                                .putExtra("item",item)
                                .putExtra("type",0);
                        startActivity(intent);
                    }
                }
            });
        }


    }

    private void initdata2() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(OkeHomeConsumptionApi.ELVDOU_BASEURL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiServer apiServer = retrofit.create(ApiServer.class);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("partnerType",6);//类型 6为会员分享  7为会员邀请
        hashMap.put("page",1);
        hashMap.put("pagesize",10);
        hashMap.put("client",2);//客户端标识
        // 获取sign签名



        final Observable<ShareMemberBean> sharemember = apiServer.sharemember(
                                                                        Constants.APPVERSION,
                                                                        Constants.TIME,
                                                                        Constants.TOKEN,
                                                                        Constants.CLIENT,
                                                                        URLEntity.getInstance().getImei(),
                                                                        Constants.PLAFORM,
                                                                        Constants.SIGN,
                                                                        hashMap);
        sharemember.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShareMemberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ShareMemberBean shareMemberBean) {
                        if(shareMemberBean!=null && shareMemberBean.getData() != null){
                            List<ShareMemberBean.DataBeanX.DataBean> data = shareMemberBean.getData().getData();
                            if (data.size()>0) {
                                shareMemberBeans.addAll(data);
                                shareMemberAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.getMessage() );
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
